package com.aquarium.ui;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.alarm.AlarmManager;
import com.aquarium.config.AlarmDefaultValue;

public class AlarmSettingActivity extends Activity implements OnClickListener,
		Observer {
	// AlarmValue alarmValue;
	AlarmManager mAlarmManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarmsetting);
		mAlarmManager = AlarmManager.getInstance(this);
		mAlarmManager.addObserver(this);
		initView();
	}

	public void initView() {
		((TextView) findViewById(R.id.mincondutivity)).setText(mAlarmManager
				.getConductivityMin() + "");
		((TextView) findViewById(R.id.maxcondutivity)).setText(mAlarmManager
				.getConductivityMax() + "");
		((TextView) findViewById(R.id.intervalcondutivity))
				.setText(mAlarmManager.getConductivityAlarmInterval() + "");
		((TextView) findViewById(R.id.minoxygen)).setText(mAlarmManager
				.getOxygenMin() + "");
		((TextView) findViewById(R.id.maxoxygen)).setText(mAlarmManager
				.getOxygenMax() + "");
		((TextView) findViewById(R.id.intervaloxygen)).setText(mAlarmManager
				.getOxygenAlarmInterval() + "");
		((TextView) findViewById(R.id.minph)).setText(mAlarmManager.getPhMin()
				+ "");
		((TextView) findViewById(R.id.maxph)).setText(mAlarmManager.getPhMax()
				+ "");
		((TextView) findViewById(R.id.intervalph)).setText(mAlarmManager
				.getPhAlarmInterval() + "");
		((TextView) findViewById(R.id.mintemperature)).setText(mAlarmManager
				.getTemperatureMin() + "");
		((TextView) findViewById(R.id.maxtemperature)).setText(mAlarmManager
				.getTemperatureMax() + "");
		((TextView) findViewById(R.id.intervaltemperature))
				.setText(mAlarmManager.getTemperatureAlarmInterval() + "");
		((TextView) findViewById(R.id.minwaterlevel)).setText(mAlarmManager
				.getWaterLevelMin() + "");
		((TextView) findViewById(R.id.maxwaterlevel)).setText(mAlarmManager
				.getWaterLevelMax() + "");
		((TextView) findViewById(R.id.intervalwaterlevel))
				.setText(mAlarmManager.getWaterLevelInterval() + "");

		findViewById(R.id.settingcondutivity).setOnClickListener(this);
		findViewById(R.id.settingoxygen).setOnClickListener(this);
		findViewById(R.id.settingph).setOnClickListener(this);
		findViewById(R.id.settingtemperature).setOnClickListener(this);
		findViewById(R.id.settingwaterlevel).setOnClickListener(this);
		findViewById(R.id.reset).setOnClickListener(this);

	}

	private void resetAlarmValue() {
		mAlarmManager
				.setConductivityAlarmInterval(AlarmDefaultValue.INTERVALConductivity);
		mAlarmManager.setConductivityMax(AlarmDefaultValue.MAXConductivity);
		mAlarmManager.setConductivityMin(AlarmDefaultValue.MINConductivity);
		mAlarmManager.setOxygenAlarmInterval(AlarmDefaultValue.INTERVALOxygen);
		mAlarmManager.setOxygenMax(AlarmDefaultValue.MAXOxygen);
		mAlarmManager.setOxygenMin(AlarmDefaultValue.MINOxygen);
		mAlarmManager.setPhAlarmInterval(AlarmDefaultValue.INTERVALPH);
		mAlarmManager.setPhMax(AlarmDefaultValue.MAXPH);
		mAlarmManager.setPhMin(AlarmDefaultValue.MINPH);
		mAlarmManager
				.setTemperatureAlarmInterval(AlarmDefaultValue.INTERVALTEMPERATURE);
		mAlarmManager.setTemperatureMax(AlarmDefaultValue.MAXTEMPERATURE);
		mAlarmManager.setTemperatureMin(AlarmDefaultValue.MINTEMPERATURE);
		mAlarmManager
				.setWaterLevelAlarmInterval(AlarmDefaultValue.INTERVALWATERLEVEL);
		mAlarmManager.setWaterLevelMax(AlarmDefaultValue.MAXWATERLEVEL);
		mAlarmManager.setWaterLevelMin(AlarmDefaultValue.MINWATERLEVEL);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		Log.d("", "" + id + "reset" + R.id.reset);
		switch (id) {
		case R.id.settingcondutivity:
			shoeDialog("settingcondutivity", id);
			break;
		case R.id.settingoxygen:
			shoeDialog("settingoxygen", id);
			break;
		case R.id.settingph:
			shoeDialog("settingph", id);
			break;
		case R.id.settingtemperature:
			shoeDialog("settingTemperature", id);
			break;
		case R.id.settingwaterlevel:
			shoeDialog("setting water level", id);
			break;

		case R.id.reset:
			resetAlarmValue();
			initView();
			break;

		default:
			break;
		}
	}

	public void shoeDialog(String title, final int viewid) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		final View view = getLayoutInflater().inflate(R.layout.alarmitem, null);
		initDialogView(view, viewid);
		// builder.setMessage("R.string.dialog_fire_missiles");
		builder.setView(view);
		builder.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						setAlarm(view, viewid);
						switch (id) {
						case R.id.settingcondutivity:
							setAlarm(view, id);
							break;
						case R.id.settingoxygen:
							setAlarm(view, id);
							break;
						case R.id.settingph:
							setAlarm(view, id);
							break;
						case R.id.settingtemperature:
							setAlarm(view, id);
							break;
						case R.id.settingwaterlevel:
							setAlarm(view, id);
							break;

						default:
							break;
						}
						// FIRE ZE MISSILES!
					}
				});
		builder.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// User cancelled the dialog
					}
				});
		// Create the AlertDialog object and return it
		builder.create();
		builder.show();
	}

	private void initDialogView(View view, int viewid) {
		EditText minEditText = (EditText) view.findViewById(R.id.min);
		EditText maxEditText = (EditText) view.findViewById(R.id.max);
		EditText intervalEditText = (EditText) view.findViewById(R.id.interval);
		switch (viewid) {
		case R.id.settingcondutivity:
			minEditText.setText(mAlarmManager.getConductivityMin() + "");
			maxEditText.setText(mAlarmManager.getConductivityMax() + "");
			intervalEditText.setText(""
					+ (int)mAlarmManager.getConductivityAlarmInterval());
			break;
		case R.id.settingoxygen:
			minEditText.setText(mAlarmManager.getOxygenMin() + "");
			maxEditText.setText(mAlarmManager.getOxygenMax() + "");
			intervalEditText.setText(""
					+ (int)mAlarmManager.getOxygenAlarmInterval());
			break;
		case R.id.settingph:
			minEditText.setText(mAlarmManager.getPhMin() + "");
			maxEditText.setText(mAlarmManager.getPhMax() + "");
			intervalEditText.setText("" +(int) mAlarmManager.getPhAlarmInterval());
			break;
		case R.id.settingtemperature:
			minEditText.setText(mAlarmManager.getTemperatureMin() + "");
			maxEditText.setText(mAlarmManager.getTemperatureMax() + "");
			intervalEditText.setText(""
					+(int) mAlarmManager.getTemperatureAlarmInterval());
			break;
		case R.id.settingwaterlevel:
			minEditText.setText(mAlarmManager.getWaterLevelInterval() + "");
			maxEditText.setText(mAlarmManager.getWaterLevelMax() + "");
			intervalEditText.setText("" +(int) mAlarmManager.getWaterLevelMin());
			break;

		default:
			break;
		}

	}

	public void setAlarm(View view, int id) {
		float min;
		float max;
		int interval;
		try {

			min = Float.valueOf(((EditText) view.findViewById(R.id.min))
					.getText().toString().trim());
			max = Float.valueOf(((EditText) view.findViewById(R.id.max))
					.getText().toString().trim());
			interval =(int)(float)Float.valueOf(((EditText) view
					.findViewById(R.id.interval)).getText().toString().trim());
		} catch (Exception e) {
			Log.e("", "  valueof error");
			return;
		}
		switch (id) {

		case R.id.settingcondutivity:
			mAlarmManager.setConductivityMin(min);
			mAlarmManager.setConductivityMax(max);
			mAlarmManager.setConductivityAlarmInterval(interval);
			initView();
			break;
		case R.id.settingoxygen:
			mAlarmManager.setOxygenMin(min);
			mAlarmManager.setOxygenMax(max);
			mAlarmManager.setOxygenAlarmInterval(interval);
			initView();
			break;
		case R.id.settingph:
			mAlarmManager.setPhMin(min);
			mAlarmManager.setPhMax(max);
			mAlarmManager.setPhAlarmInterval(interval);
			initView();
			break;
		case R.id.settingtemperature:
			mAlarmManager.setTemperatureMin(min);
			mAlarmManager.setTemperatureMax(max);
			mAlarmManager.setTemperatureAlarmInterval(interval);
			initView();
			break;
		case R.id.settingwaterlevel:
			mAlarmManager.setWaterLevelAlarmInterval(interval);
			mAlarmManager.setWaterLevelMax(max);
			mAlarmManager.setWaterLevelMin(min);
			initView();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("", "onResume");

		initView();
	}

	@Override
	public void update(Observable observable, Object data) {

	}
}
